"""
实体识别基础类
定义所有实体识别模型需要实现的通用接口
"""
from abc import ABC, abstractmethod
from typing import Dict, List, Any, Optional, Tuple


class BaseEntityRecognizer(ABC):
    """实体识别基类，定义统一接口"""
    
    def __init__(self, config: Dict[str, Any]):
        """
        初始化实体识别模型
        
        Args:
            config: 模型配置参数
        """
        self.config = config
        self.model = None
        self.tokenizer = None
        
    @abstractmethod
    def load_model(self, model_path: str) -> bool:
        """
        加载预训练模型
        
        Args:
            model_path: 模型路径
        
        Returns:
            bool: 加载是否成功
        """
        pass
        
    @abstractmethod
    def save_model(self, model_path: str) -> bool:
        """
        保存模型
        
        Args:
            model_path: 模型保存路径
        
        Returns:
            bool: 保存是否成功
        """
        pass
        
    @abstractmethod
    def train(self, train_data: List[Dict[str, Any]], val_data: Optional[List[Dict[str, Any]]] = None) -> Dict[str, float]:
        """
        训练模型
        
        Args:
            train_data: 训练数据
            val_data: 验证数据
        
        Returns:
            Dict[str, float]: 训练指标
        """
        pass
        
    @abstractmethod
    def predict(self, text: str) -> List[Dict[str, Any]]:
        """
        预测文本中的实体
        
        Args:
            text: 输入文本
        
        Returns:
            List[Dict[str, Any]]: 实体列表，每个实体包含类型、起始位置、结束位置和文本
        """
        pass
        
    @abstractmethod
    def evaluate(self, test_data: List[Dict[str, Any]]) -> Dict[str, float]:
        """
        评估模型性能
        
        Args:
            test_data: 测试数据
        
        Returns:
            Dict[str, float]: 评估指标
        """
        pass
        
    @abstractmethod
    def convert_to_features(self, data: List[Dict[str, Any]]) -> Any:
        """
        将数据转换为模型输入特征
        
        Args:
            data: 原始数据
        
        Returns:
            Any: 模型输入特征
        """
        pass


class EntityRecognizerFactory:
    """实体识别模型工厂类，用于创建不同类型的实体识别模型"""
    _recognizer_implementations = {}
    
    @classmethod
    def register_recognizer(cls, recognizer_type: str, recognizer_class):
        """
        注册实体识别模型实现
        
        Args:
            recognizer_type: 模型类型
            recognizer_class: 模型类
        """
        cls._recognizer_implementations[recognizer_type] = recognizer_class
        
    @classmethod
    def create_recognizer(cls, recognizer_type: str, config: Dict[str, Any]) -> BaseEntityRecognizer:
        """
        创建实体识别模型实例
        
        Args:
            recognizer_type: 模型类型
            config: 模型配置
        
        Returns:
            BaseEntityRecognizer: 实体识别模型实例
        
        Raises:
            ValueError: 模型类型不存在
        """
        if recognizer_type not in cls._recognizer_implementations:
            raise ValueError(f"不支持的实体识别模型类型: {recognizer_type}")
        
        return cls._recognizer_implementations[recognizer_type](config)